package nl.rainbowsheep.reisorg.cust.site.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.cust.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.cust.site.model.TripBooking;
import nl.rainbowsheep.reisorg.cust.site.viewmodel.TripBookingViewmodel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class TripBookingService {

    @Value("${api.url}")
    String url;

    public TripBooking save(TripBooking tripBooking) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        
        tripBooking.setId(-1);
        TripBooking savedTripBooking = template.postForObject(url + "trip/booking/save", tripBooking, TripBooking.class);
        return savedTripBooking;
    }
    
    public TripBookingViewmodel[] getByUsername(String username) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);
        
        TripBookingViewmodel[] tripbookingvm = template.getForObject(url + "trip/booking/getallbyusername?username=" + username, TripBookingViewmodel[].class); 
        return tripbookingvm;
    }
}
